package com.nj.dms.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.dao.FileDao;
import com.nj.dms.dao.impl.FileDaoImpl;

/**
 * 删除文件
 */
@WebServlet("/DeleteFile")
public class DeleteFile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取用户ID
		int id = new Integer(request.getParameter("id"));

		System.out.println("删除文件:" + id);

		FileDao fileDao = new FileDaoImpl();
		String path = fileDao.getFilePath(id);

		// 删除文件数据
		fileDao.deleteFile(id);

		// 获取服务器真实路径

		String serPath = request.getServletContext().getRealPath("/") + "uploads" + File.separator + path;

		// 同时删除文件资源
		File file = new File(serPath);

		System.out.println("文件路径：" + serPath);

		if (file.exists()) {
			// 文件存在，即删除文件

			System.out.println("文件存在");

			file.delete();

		} else {

			System.out.println("文件不存在");

		}

		// 返回到文件列表
		response.sendRedirect("MyFileList");

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
